package com.newlink.building.activities.main.dialer.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.newlink.building.NL_App
import com.newlink.building.R
import com.newlink.building.activities.main.history.data.Module_Phone_GroupedCallLogData
import com.newlink.building.activities.main.adapter.Module_Phone_SelectionListAdapter
import com.newlink.building.activities.main.viewmodels.Module_Phone_ListTopBarViewModel
import com.newlink.building.databinding.GenericListHeaderBinding
import com.newlink.building.databinding.ItemDialerHistoryBinding
import com.newlink.building.utils.Module_Phone_AppUtils
import com.newlink.building.utils.Module_Phone_Event
import com.newlink.building.utils.HeaderAdapter
import org.linphone.core.CallLog

/**
 * @author wangyang
 * @date 2022/11/17
 * @description 拨号页面-拨号历史列表的adapter
 */
class Module_Phone_DialerHistoryAdapter(
    selectionVM: Module_Phone_ListTopBarViewModel,
    private val viewLifecycleOwner: LifecycleOwner
) : Module_Phone_SelectionListAdapter<Module_Phone_GroupedCallLogData, RecyclerView.ViewHolder>(
    selectionVM,
    DialerHistoryDiffCallback()
),
    HeaderAdapter {

    val selectedDialerHistoryEvent: MutableLiveData<Module_Phone_Event<CallLog>> by lazy {
        MutableLiveData<Module_Phone_Event<CallLog>>()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): Module_Phone_DialerHistoryAdapter.ViewHolder {
        val binding: ItemDialerHistoryBinding = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.item_dialer_history, parent, false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as ViewHolder).bind(getItem(position))
    }

    inner class ViewHolder(
        val binding: ItemDialerHistoryBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(groupedCallLogData: Module_Phone_GroupedCallLogData) {
            with(binding) {
                callLogData = groupedCallLogData

                lifecycleOwner = viewLifecycleOwner

                // This is for item selection through ListTopBarFragment
                selectionListViewModel = selectionViewModel
                selectionViewModel.isEditionEnabled.observe(
                    viewLifecycleOwner
                ) {
                    position = bindingAdapterPosition
                }

                setClickListener {
                    if (selectionViewModel.isEditionEnabled.value == true) {
                        selectionViewModel.onToggleSelect(bindingAdapterPosition)
                    } else {
                        NL_App.coreContext.startCall(groupedCallLogData.lastCallLogData.getSipLine())
                    }
                }

//                setLongClickListener {
//                    if (selectionViewModel.isEditionEnabled.value == false) {
//                        selectionViewModel.isEditionEnabled.value = true
//                        // Selection will be handled by click listener
//                        true
//                    }
//                    false
//                }

                executePendingBindings()
            }
        }
    }

    private class DialerHistoryDiffCallback : DiffUtil.ItemCallback<Module_Phone_GroupedCallLogData>() {
        override fun areItemsTheSame(
            oldItem: Module_Phone_GroupedCallLogData,
            newItem: Module_Phone_GroupedCallLogData
        ): Boolean {
            return newItem.lastCallLog?.callId?.let { oldItem.lastCallLog?.callId?.compareTo(it) } == 0
        }

        override fun areContentsTheSame(
            oldItem: Module_Phone_GroupedCallLogData,
            newItem: Module_Phone_GroupedCallLogData
        ): Boolean {
            return false // For headers
        }
    }

    override fun displayHeaderForPosition(position: Int): Boolean {
        if (position >= itemCount) return false
        val groupedCallLogData = getItem(position)
        val firstLetter = groupedCallLogData.lastCallLog?.callId.toString()
        val previousPosition = position - 1
        return if (previousPosition >= 0) {
            val previousItemFirstLetter = getItem(previousPosition).lastCallLog?.callId.toString()
            !firstLetter.equals(previousItemFirstLetter, ignoreCase = true)
        } else true
        return false
    }

    override fun getHeaderViewForPosition(context: Context, position: Int): View {
        val groupedCallLogData = getItem(position)
        val firstLetter = Module_Phone_AppUtils.getInitials(groupedCallLogData.lastCallLog?.callId.toString(), 1)
        val binding: GenericListHeaderBinding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.generic_list_header, null, false
        )
        binding.title = firstLetter
        binding.executePendingBindings()
        return binding.root
    }
}
